package com.hndzyu.yd.team;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.Team;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.ui.LoginActivity;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zz9527 on 2018/10/11.
 */
public class TeamFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.my_today)
    TextView my_today;
    @BindView(R.id.my_month)
    TextView my_month;
    @BindView(R.id.my_all)
    TextView my_all;
    @BindView(R.id.team_today)
    TextView team_today;
    @BindView(R.id.team_month)
    TextView team_month;
    @BindView(R.id.team_all)
    TextView team_all;
    @BindView(R.id.vip_today)
    TextView vip_today;
    @BindView(R.id.vip_month)
    TextView vip_month;
    @BindView(R.id.vip_all)
    TextView vip_all;
    @BindView(R.id.all)
    TextView all;
    @BindView(R.id.will_all)
    TextView will_all;
    @BindView(R.id.ll_team)
    LinearLayout ll_team;
    @BindView(R.id.nologin)
    ImageView nologin;
    @BindView(R.id.invite)
    TextView invite;
    @BindView(R.id.novip)
    TextView novip;
    @BindView(R.id.zhishu)
    TextView zhishu;

    Unbinder unbinder1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_team, container, false);
        unbinder1 = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        initImmersionBar();
        nologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });
        if (UserBean.uid == 0) {
//            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            ll_team.setVisibility(View.VISIBLE);
            getData();
        }
//        if (UserBean.pid != 0) {
//            liuxiu.setVisibility(View.VISIBLE);
//        }
        invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(getActivity(), PopPosterActivity.class));
                Intent i = new Intent();
                i.setClass(getActivity(), PopPosterActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        });
    }

    protected ImmersionBar mImmersionBar;

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (UserBean.uid == 0) {
            ll_team.setVisibility(View.GONE);
        } else {
            ll_team.setVisibility(View.VISIBLE);
            getData();
        }
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarDarkFont(true, 0.2f)
                    .init();
    }


    private void getData() {

        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.Team(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        Team.DataBean data = gson.fromJson(json, Team.class).getData();
                        my_today.setText(data.getOneself().getDaybalance());
                        my_month.setText(data.getOneself().getMonthbalance());
                        my_all.setText(data.getOneself().getTotalbalance());
                        if (data.getSum() == 0) {
                            will_all.setText("0");
                        } else {
                            will_all.setText(data.getSum() + "");
                        }
                        if (data.getTeam().getRevenueToday() == 0) {
                            team_today.setText("0");
                        } else {
                            team_today.setText(data.getTeam().getRevenueToday() + "");
                        }
                        if (data.getTeam().getRevenueMonth() == 0) {
                            team_month.setText("0");
                        } else {
                            team_month.setText(data.getTeam().getRevenueMonth() + "");
                        }
                        if (data.getTeam().getRevenueTotal() == 0) {
                            team_all.setText("0");
                        } else {
                            team_all.setText(data.getTeam().getRevenueTotal() + "");
                        }
                        vip_today.setText(data.getTeam().getVipToday() + "");
                        vip_month.setText(data.getTeam().getVipMonth() + "");
                        vip_all.setText(data.getTeam().getVipTotal() + "");
                        all.setText(data.getTeam().getTeamTotal() + "");
                        novip.setText(data.getTeam().getTeamTotal()-data.getTeam().getVipTotal()+"");

                    } catch (Exception e) {
                    }
                } else {
                    Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

    @OnClick({R.id.team_today, R.id.team_month, R.id.team_all, R.id.vip_today, R.id.vip_month, R.id.vip_all, R.id.all, R.id.will_all, R.id.zhishu,R.id.novip})
    public void onViewClicked(View view) {
        Intent intent = new Intent(getActivity(), TeamDeatilActivity.class);
        switch (view.getId()) {
            case R.id.team_today:
                intent.putExtra("type", 1);
                break;
            case R.id.team_month:
                intent.putExtra("type", 2);
                break;
            case R.id.team_all:
                intent.putExtra("type", 3);
                break;
            case R.id.vip_today:
                intent.putExtra("type", 4);
                break;
            case R.id.vip_month:
                intent.putExtra("type", 5);
                break;
            case R.id.vip_all:
                intent.putExtra("type", 6);
                break;
            case R.id.all:
                intent.putExtra("type", 7);
                break;
            case R.id.will_all:
                intent.putExtra("type", 8);
                break;
            case R.id.novip:
                intent.putExtra("type", 9);
                break;
            case R.id.zhishu:
                startActivity(new Intent(getContext(), ZhiShuActivity.class));
                return;
            default:
                break;

        }
        startActivity(intent);


    }
    Context context;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.context=activity;
    }
}

